﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Agency_BidSwitch : System.Web.UI.Page
{
    int projectid;


    protected void Page_Load(object sender, EventArgs e)
    {
        //   projectid = 1;
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            projectid = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["projectid"]));
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            //开启全部投标企业投标开关,BiddingSwitching=0为关闭BiddingSwitching=1为开启
           
            if (Request.Form["Btnopenall"] == "全部开启投标")
            {
                Db db = new Db();
                string str = "update ProjectCompanyAttribution set BiddingSwitching='1' where projectid=" + projectid;
                db.Command(str);
                db.Close();
                //HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                Response.Write("<script>window.alert('全部开启投标成功');</script>");
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", str, "../Agency/BidSwitch.aspx");
                showdata();
            }

            //关闭全部投标企业投标开关,BiddingSwitching=0为关闭BiddingSwitching=1为开启

            if (Request.Form["Btncloseall"] == "全部关闭投标")
            {
                Db db = new Db();
                string str = "update ProjectCompanyAttribution set BiddingSwitching='0' where projectid=" + projectid;
                db.Command(str);
                db.Close();
                Response.Write("<script>window.alert('全部关闭投标成功');</script>");
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", str, "../Agency/BidSwitch.aspx");
                showdata();
            }


            //开启选定企业投标开关BiddingSwitching=1
            if (Request.Form["openbj"] == "开启投标")
            {
                if (Request.Form["id"] != null)
                {
                    int intProjectId = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["id"]));
                    string[] strId = Request.Form["id"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

                    if (strId.Length > 0)
                    {
                        string strSql, sql = "";
                        Db db = new Db();

                        foreach (string Id in strId)
                        {
                            try
                            {
                                strSql = SysFun.ToTrim(Id);
                                sql = strSql + "," + sql;

                            }
                            catch { }
                        }
                        sql = sql.Substring(0, sql.Length - 1);
                        sql = "update ProjectCompanyAttribution set BiddingSwitching='1' where projectid=" + projectid + " and id in(" + sql + ")";
                        db.Command(sql);
                        db.Close();
                        Response.Write("<script>window.alert('开启投标成功');</script>");
                        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", sql, "../Agency/BidSwitch.aspx");
                        showdata();

                    }
                    else
                    {
                        Response.Write("");
                    }

                }
            }


            //关闭选定企业投标开关BiddingSwitching=0
            if (Request.Form["closebj"] == "关闭投标")
            {
                if (Request.Form["id"] != null)
                {
                    int intProjectId = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["id"]));
                    string[] strId = Request.Form["id"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

                    if (strId.Length > 0)
                    {
                        string strSql, sql = "";
                        Db db = new Db();
                        foreach (string Id in strId)
                        {
                            try
                            {
                                strSql = SysFun.ToTrim(Id);
                                sql = strSql + "," + sql;

                            }
                            catch { }
                        }
                        sql = sql.Substring(0, sql.Length - 1);
                        sql = "update ProjectCompanyAttribution set BiddingSwitching='0' where projectid=" + projectid + " and id in(" + sql + ")";
                        db.Command(sql);
                        db.Close();
                        //closebj.Dispose();
                        Response.Write("<script>window.alert('关闭成功');</script>");
                        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", sql, "../Agency/BidSwitch.aspx");
                        showdata();
                    }
                    else
                    {
                        Response.Write("");
                    }
                }
            }
            showdata();
        }


    }

    //datagridview数据绑定
    protected void showdata()
    {
        Db db = new Db();
        DbPage dbPage = new DbPage(db);

        //string strGetPageLink = "";

        dbPage.Table = "ProjectCompanyAttribution,Company";                                               //表名，可以多个表关联
        dbPage.Key = "Id";
        //该查询可以唯一表示记录的字段，该属性必须设置且只能有一个唯一标识字段
        //        if (!IsPostBack)
        string sql = "";
        if (ComPanyName.Text.Trim().Length > 0)
        {
            sql = "( CompanyName Like '%" + ComPanyName.Text.Trim() + "%' or UserCode like '%" + ComPanyName.Text.Trim() + "%') and ";// or UserCode like '%" + SysFun.ToTrim(Request.Form["CompanyName"]) + "%'
            //strGetPageLink = "&CompanyName=" + Server.UrlEncode(SysFun.ToTrim(Request.Form["CompanyName"]));
        }
        dbPage.Where = sql + " projectid=" + projectid + " and ProjectCompanyAttribution.AccountId=Company.AccountId ";

        dbPage.SelectField = "Id,CompanyName,BiddingSwitching,usercode ";                        //可以不设，默认为“*”


        dbPage.OrderBy = "CompanyName";                                //排序字段，可以不输入。不输入时以 Key 作为排序字段

        dbPage.PageIndex = SysFun.ToInt(Request.QueryString["PG"]);             //设置需要查询第几页
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]); //设置需要查询第几页
        dbPage.PageSize = 30;                                                   //设置每页显示多少条记录



        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        //    Response.Write("<p align=center>投标人审核界面</p>");
        sb.Append("<table border=1 cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' width='98%'>");
        sb.Append("<tr class='table_title' nowrap>");
        sb.Append("<td class='table_title' nowrap>选取</td>");
        sb.Append("<td class='table_title' nowrap>投标人</td>");
        sb.Append("<td class='table_title' nowrap>投标人注册号</td>");
        sb.Append("<td class='table_title' nowrap>投标状态</td>");
        //     sb.Append("<td nowrap align=center>选择</td>");
        sb.Append("</tr>");

        int intNum = 0, i, j = 0;
        string zhuangtai = "";//zhuangtai 表示审核状态


        System.Data.IDataReader dr = dbPage.ExecuteReader();
        while (dr.Read())
        {
            intNum++;
            i = SysFun.ToInt(dr["BiddingSwitching"]);
            if (i == 0)
            {
                zhuangtai = "关";
            }
            if (i == 1)
            {
                zhuangtai = "开";
            }
            if (j == 0)
            {
                sb.Append("<tr class='table_td'>");
                j++;
            }
            else
            {
                sb.Append("<tr class='table_td2'>");
                j--;
            }



            sb.Append("<td><input  type='checkbox' name='id'value='" + SysFun.ToTrim(dr["id"]) + "'></td>");
            sb.Append("<td align='center'>" + SysFun.ToTrim(dr["CompanyName"]) + "</td>");
            sb.Append("<td align='center'>" + SysFun.ToTrim(dr["usercode"]) + "</td>");
            sb.Append("<td align='center'>" + zhuangtai + "</td>");
            //      sb.Append("<td>" + SysFun.ToTrim(dr["SpellingCode"]) + "</td>");
            sb.Append("</tr>");
        }
        dr.Close();

        sb.Append("</table>");

        this.DbPageFlip1.DbPage = dbPage;

        //sb.Append("<p align='right'>" + dbPage.GetPageLink("BidTableProductSelect.aspx?PG={PageNum}" + strGetPageLink) + "</p>");

        //Label lab = new Label();
        Label1.Text = sb.ToString();
        //frmMain.Controls.Add(lab);
        db.Close();
    }
    //导入报价人
    protected void Button1_Click(object sender, EventArgs e)
    {
         string str="";
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (projectid == 1) // 1为基本药品
        {
            str = @"insert ProjectCompanyAttribution(AccountId,ProjectId)
                              select distinct a.AccountId_TJR," + projectid + " from (select distinct AccountId_TJR from goods UNION ALL select distinct AccountId_TJR from Goods) as a where AccountId_TJR not in(select AccountId from ProjectCompanyAttribution where ProjectId=" + projectid + ")";
        }
        else
        {
            str = @"insert ProjectCompanyAttribution(AccountId,ProjectId)
                              select distinct a.AccountId_TJR," + projectid + " from (select distinct AccountId_TJR from GoodsByCompany  UNION ALL select distinct AccountId_TJR from Goods) as a where AccountId_TJR not in(select AccountId from ProjectCompanyAttribution where ProjectId=" + projectid + ")";
     
        }
            Db db = new Db();
        db.Command(str);
        db.Close();
        Response.Write("<script>window.alert('导入报价人成功');</script>");
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "ProjectCompanyAttribution", str, "../Agency/BidSwitch.aspx");
        showdata();
    }
    //开启报价 审核未通过的企业
    protected void Button2_Click(object sender, EventArgs e)
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        string str = "update ProjectCompanyAttribution set BiddingSwitching=1 from Company where ProjectCompanyAttribution.AccountId=Company.AccountId and CheckStatus=2 and ProjectId="+projectid;
        Db db = new Db();
        db.Command(str);
        db.Close();
        Response.Write("<script>window.alert('审核未通过企业开启报价成功');</script>");
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "ProjectCompanyAttribution", str, "../Agency/BidSwitch.aspx");
        showdata();
    }
    //关闭报价 审核未通过的企业
    protected void Button3_Click(object sender, EventArgs e)
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        string str = "update ProjectCompanyAttribution set BiddingSwitching=0 from Company where ProjectCompanyAttribution.AccountId=Company.AccountId and CheckStatus=2 and ProjectId=" + projectid;
        Db db = new Db();
        db.Command(str);
        db.Close();
        Response.Write("<script>window.alert('审核未通过企业关闭报价成功');</script>");
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "ProjectCompanyAttribution", str, "../Agency/BidSwitch.aspx");
        showdata();
    }
}

